package com.ivan.stu.notetool.utils;

import android.content.Context;
import android.text.InputFilter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.CharacterStyle;
import android.text.style.ImageSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.widget.EditText;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IVAN on 2019/3/6.
 */

public class EditTextUtil {

    public static  void setEditTextInputSpace(final Context context, EditText editText, int flag) {

        if (flag == 0){

            InputFilter filter = new InputFilter() {

                Pattern emoji = Pattern.compile(
                        "[^a-zA-Z0-9\\u4E00-\\u9FA5_,.?!:;…~_\\-\"\"/@*+'()<>{}/[/]()<>{}\\[\\]=%&$|\\/♀♂#¥£¢€\"^` ，。？！：；……～“”、“（）”、（——）‘’＠‘·’＆＊＃《》￥《〈〉》〈＄〉［］￡［］｛｝｛｝￠【】【】％〖〗〖〗／〔〕〔〕＼『』『』＾「」「」｜﹁﹂｀．]",
                        Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
                @Override
                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    Matcher emojiMatcher = emoji.matcher(source);

                    if (emojiMatcher.find()){

                        TextViewUtils.showToast(context,1,"禁止输入非法字符！");

                        return "";

                    }else {

                        return null;
                    }
                }
            };

            editText.setFilters(new InputFilter[]{filter,new InputFilter.LengthFilter(50)});

        }else {

            InputFilter filter = new InputFilter() {

                Pattern emoji = Pattern.compile(
                        "[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]",
                        Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
                @Override
                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    Matcher emojiMatcher = emoji.matcher(source);

                    if (emojiMatcher.find()){

                        TextViewUtils.showToast(context,1,"禁止输入非法字符！");

                        return "";

                    }else {

                        return null;

                    }

                }
            };
            editText.setFilters(new InputFilter[]{filter});
        }
    }


    //设置不可点击

    public static void setCanNotEditNoClick(EditText view) {
        view.setFocusable(false);
        view.setFocusableInTouchMode(false);

    }


    //设置可点击

    public static void setCanEdit(EditText view) {
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
    }


    //提交保存笔记内容的时候需要重新插入字体加粗和下划线标签。
    public static String convertSpannedToRichText(Spanned spanned) {

        List<CharacterStyle> spanList =
                Arrays.asList(spanned.getSpans(0, spanned.length(), CharacterStyle.class));  //获取上传内容中所有的span
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(spanned);

        Log.e("spanList",spanList.size()+"");
        for (CharacterStyle characterStyle : spanList) {

            int start = stringBuilder.getSpanStart(characterStyle);//当前span的开始位置
            int end = stringBuilder.getSpanEnd(characterStyle);  //当前span的结束位置

            Log.e("start = end",start+"="+end);

            if (start >= 0) {
                String htmlStyle = handleCharacterStyle(characterStyle,
                        stringBuilder.subSequence(start, end).toString());
                if (htmlStyle != null) {
                    stringBuilder.replace(start, end, htmlStyle);   //替换之前的span所覆盖的内容
                }
            }
        }
        return stringBuilder.toString();
    }


    private static String handleCharacterStyle(CharacterStyle characterStyle, String text) {

        if (characterStyle instanceof StyleSpan) {
            return String.format("<strong>%s</strong>", text);
        } else if (characterStyle instanceof UnderlineSpan) {
            return String.format("<span style=\"%s\">%s</span>","text-decoration: underline;",text);
        }else if (characterStyle instanceof ImageSpan){

            return text;
        }
        return null;
    }
}
